package com.xiangmo.travel.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangmo.travel.Dto.LikeDto;
import com.xiangmo.travel.Entity.CommunityLike;
import com.xiangmo.travel.Entity.Like;
import com.xiangmo.travel.Service.LikeService;
import com.xiangmo.travel.mapper.LikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private LikeMapper likeMapper;
    @Override
    public int addLike(LikeDto likeDto) {
        Like like = Like.builder()
                .commentId(likeDto.getCommentId())
                .userId(likeDto.getUserId())
                .build();
        return likeMapper.insert(like);
    }

    @Override
    public int deleteLike(LikeDto likeDto) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getCommentId, likeDto.getCommentId())
                        .and(i -> i.eq(Like::getUserId, likeDto.getUserId()));
        return likeMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public Long getCountByCommentId(Long commentId) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.apply("`like`.comment_id = {commentId}", commentId);
        lambdaQueryWrapper.eq(Like::getCommentId, commentId);
        return likeMapper.selectCount(lambdaQueryWrapper);
    }

    @Override
    public int checkCommentIsLike(LikeDto likeDto) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getUserId, likeDto.getUserId())
                .and(i -> i.eq(Like::getCommentId, likeDto.getCommentId()));
        Like like = likeMapper.selectOne(lambdaQueryWrapper);
        return like == null ? 0 :1;
    }
}
